# write by hasaki
# first  edit on 2019/04/21
# last change on 2019/05/03
# 更新tick的信息
from datetime import datetime
import json

from hashakisea.strategy.haStrategyTemplate import HaStrategy

class TickMsg(HaStrategy):
    def __init__(self):     
        super(TickMsg,self).__init__()
        self.strategy_name='tick msg'
        self.save_json_path='C:/Users/cchyp/Desktop/hasaki/quote/last_day/data.json'
        self.counter_save_time=0
    
    def next(self,data):
        '''
        tick数据：
        {"data": {"quoteVolume": "1141795.9880448482", "baseVolume": "2739104.51651617209508", 
        "highestBid": "2.3948", "high24hr": "2.435", "last": "2.3948", "lowestAsk": "2.3959", "elapsed":
         "10ms", "result": "true", "low24hr": "2.3563", "percentChange": "1.5821845174974", "target_symbol": "eos_usdt"}, "type": "tick"}
        '''
        percent=float(data['percentChange'])
        low=round(float(data['low24hr']) *6.8)
        high=round(float(data['high24hr'])*6.8)
        last=round(float(data['last'])*6.8)

        if self.counter_save_time==0:
            # 读取
            with open(self.save_json_path,'r',encoding='utf-8') as json_file:
                json_data=json.load(json_file)

            # 更新数据
            json_data['precent']=percent
            json_data['high']=high
            json_data['low']=low
            json_data['now_price']=last

            # 写入
            with open(self.save_json_path,'w',encoding='utf-8') as json_file:
                json.dump(json_data,json_file,ensure_ascii=False)
            
                
        elif self.counter_save_time==20:
            self.counter_save_time=-1
        
        self.counter_save_time+=1
            
    
    def onOrder(self):
        pass
    
    def onTrade(self):
        pass